package cn.longwi.lwse.config;


import cn.longwi.lwse.controller.LicenseController;
import cn.longwi.lwse.handler.LicenseInterceptor;
import cn.longwi.lwse.properties.LicenseProperties;
import cn.longwi.lwse.service.LicenseService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * LicenseAutoConfiguration
 * @author yyq
 * @date 2024/10/10
 */
@Configuration
@EnableConfigurationProperties(LicenseProperties.class)
public class LicenseAutoConfiguration {

    @Bean
    public LicenseService licenseService() {
        return new LicenseService();
    }

    @Bean
    public LicenseInterceptor licenseInterceptor() {
        return new LicenseInterceptor();
    }

    @Bean
    @ConditionalOnMissingBean
    public LicenseController licenseController() {
        return new LicenseController();
    }

    @Bean
    public WebMvcConfigurer licenseWebMvcConfigurer(LicenseInterceptor licenseInterceptor) {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(licenseInterceptor).addPathPatterns("/**")
                        .excludePathPatterns("/license","/submitLicense","/css/**","/js/**");
            }
        };
    }
}
